package com.hongtu.supplychain.finance.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;


@Component
public class JedisPlugin {


    @Autowired
    public JedisPool jedisPool   = null;


    /***
     *
     * 将数据放入Redis中缓存<br>
     *
     * @author      LiJie
     * @param key           键
     * @param value         值
     * @return
     */
    public boolean set(String key,String value){
        return set(key,value,0);
    }


    /***
     *
     * 将数据放入Redis<br>
     * 当time为0的时候为永久缓存
     *
     * @author      LiJie
     * @param key           键
     * @param value         值
     * @param time          缓存时间
     *
     */
    public boolean set(String key,String value,long time){
        Jedis jedis = null;
        boolean ret = false;
        try {
            jedis = jedisPool.getResource();
            jedis.set(key, value);
            if (time > 0) {
                jedis.pexpire(key, time);
            }
            jedisPool.returnResource(jedis);
            ret = true;
        }catch (Exception e){
            ret = false;
            if(jedis != null){
                jedisPool.returnBrokenResource(jedis);
            }
        }
        return ret;
    }


    /***
     *
     * 根据Key取出Redis中的数据
     *
     * @author      LiJie
     * @param key
     * @return
     */
    public String get(String key){
        Jedis       jedis   = null;
        String      value   = null;
        try {
            jedis = jedisPool.getResource();
            value = jedis.get(key);
            jedisPool.returnResource(jedis);
        }catch (Exception e){
            if(jedis != null){
                jedisPool.returnBrokenResource(jedis);
            }
        }
        return value;
    }


    /***
     *
     * 获取缓存剩余有效期,返回值应该大于等于0
     *
     * @author      LiJie
     * @param key
     * @return
     */
    public long ttl(String key){
        Jedis   jedis = null;
        long    time  = -1;
        try{
            jedis = jedisPool.getResource();
            time  = jedis.ttl(key);
            jedisPool.returnResource(jedis);
        }catch (Exception e){
            if(jedis != null){
                jedisPool.returnBrokenResource(jedis);
            }
        }
        return time;
    }


    /***
     *
     * 移除相应缓存
     *
     * @author      LiJie
     * @param key
     * @return
     */
    public boolean del(String key){
        Jedis   jedis   = null;
        boolean ret     = false;

        try{
            jedis = jedisPool.getResource();
            jedis.del(key);
            ret = true;
            jedisPool.returnResource(jedis);
        }catch (Exception e){
            if(jedis != null){
                jedisPool.returnBrokenResource(jedis);
            }
        }
        return ret;
    }


    /***
     *
     * 检查相应的Key是否存在
     *
     * @author      LiJie
     * @param key
     * @return
     */
    public boolean exists(String key){
        Jedis   jedis   = null;
        boolean ret     = false;

        try{
            jedis = jedisPool.getResource();
            ret = jedis.exists(key);
            jedisPool.returnResource(jedis);
        }catch (Exception e){
            if(jedis != null){
                jedisPool.returnBrokenResource(jedis);
            }
        }
        return ret;
    }

}
